<html>
<head><meta charset="utf-8"><title>mir generated for declarations inside unreachable code? · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html">mir generated for declarations inside unreachable code?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204373494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373494">(Jul 19 2020 at 22:26)</a>:</h4>
<p>I was messing around a bit and it seems that mir is generated for function declarations inside dead code (e..g. <code>if false { ..}</code>), is that something that could be skipped?<br>
<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e0b5d25c6b4554f4d1c06c9dcc4a307f">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e0b5d25c6b4554f4d1c06c9dcc4a307f</a></p>



<a name="204373508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373508">(Jul 19 2020 at 22:27)</a>:</h4>
<p>Yes!</p>



<a name="204373512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373512">(Jul 19 2020 at 22:28)</a>:</h4>
<p>Did it generate that after all the mir-opts?</p>



<a name="204373556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373556">(Jul 19 2020 at 22:28)</a>:</h4>
<p>hm, I only looked at godbolt and playground so far</p>



<a name="204373680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373680">(Jul 19 2020 at 22:33)</a>:</h4>
<p>I think it's still there?<br>
<code>fn foo::&lt;impl at bad.rs:6:9: 10:10&gt;::to_string(_1: &amp;foo::A) -&gt; std::string::String {</code><br>
that's with mirO3</p>



<a name="204373747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373747">(Jul 19 2020 at 22:35)</a>:</h4>
<p>MIR is built for every function to borrow-check it</p>



<a name="204373749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373749">(Jul 19 2020 at 22:35)</a>:</h4>
<p>It's also optimized for every function to show const prop warnings</p>



<a name="204373751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373751">(Jul 19 2020 at 22:35)</a>:</h4>
<p>aaah so the compiler can reject dead-but-invalid code?</p>



<a name="204373788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373788">(Jul 19 2020 at 22:36)</a>:</h4>
<p>Yeah</p>



<a name="204373799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204373799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204373799">(Jul 19 2020 at 22:36)</a>:</h4>
<p>I also think it wouldn't save much time since most functions are reachable</p>



<a name="204374212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204374212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204374212">(Jul 19 2020 at 22:50)</a>:</h4>
<p>For what it's worth: LLVM does eliminate dead code, so this is only an issue of whether or not to eliminate it at MIR level. Are we all on that boat? :3</p>



<a name="204374632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204374632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204374632">(Jul 19 2020 at 23:02)</a>:</h4>
<blockquote>
<p>LLVM does eliminate dead code</p>
</blockquote>
<p>mmmh <a href="https://rust.godbolt.org/z/T4bMnj">https://rust.godbolt.org/z/T4bMnj</a></p>



<a name="204375484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204375484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204375484">(Jul 19 2020 at 23:29)</a>:</h4>
<p>the actual answer is "probably not" because Rust doesn’t really preserve distinction of items being declared in dead or not dead code.</p>



<a name="204375488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204375488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204375488">(Jul 19 2020 at 23:29)</a>:</h4>
<p>only the resolver is affected by scoping of items.</p>



<a name="204375538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204375538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204375538">(Jul 19 2020 at 23:30)</a>:</h4>
<p>and since implementations of public traits at least generate public symbols, there's no way for LLVM (or rust itself, at that point) to know that these aren’t really used. I think...</p>



<a name="204375541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204375541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204375541">(Jul 19 2020 at 23:30)</a>:</h4>
<p>if there was an actual analysis that could nuke these public functions/methods, it'd be fairly involved.</p>



<a name="204375548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204375548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204375548">(Jul 19 2020 at 23:31)</a>:</h4>
<p>In this particular instance it would be necessary to prove that it is impossible to construct an instance of <code>A</code> anywhere.</p>



<a name="204375754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204375754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204375754">(Jul 19 2020 at 23:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="217864">matthiaskrgr</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F/near/204374632">said</a>:</p>
<blockquote>
<blockquote>
<p>LLVM does eliminate dead code</p>
</blockquote>
<p>mmmh <a href="https://rust.godbolt.org/z/T4bMnj">https://rust.godbolt.org/z/T4bMnj</a></p>
</blockquote>
<p>I don't think that counts as dead code on a technicality?</p>
<p>Like, yes, that <code>if</code> will never be executed. But all the names declared inside it will enter the binary's namespace. I'm pretty sure that's the case for Rust.</p>
<p>The only thing that will not get executed is the <code>let x = 8;</code> part, which is getting removed from the compiled artifact.</p>



<a name="204375770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204375770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204375770">(Jul 19 2020 at 23:39)</a>:</h4>
<p>If you for example remove all name declarations and try to do a print and stuff, all of that gets removed: <a href="https://rust.godbolt.org/z/1sMjhv">https://rust.godbolt.org/z/1sMjhv</a></p>



<a name="204375830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204375830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204375830">(Jul 19 2020 at 23:41)</a>:</h4>
<p>(btw, I changed the flag to check what happened if we upped it from opt-level 2 (=== to -O) to opt-level 3)</p>



<a name="204375949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204375949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204375949">(Jul 19 2020 at 23:45)</a>:</h4>
<p>And what nagisa is saying is (implicitly) that the names / symbols being declared are public.<br>
And then, that in order to actually remove them you'd need something like a global analysis that proved that those symbols were never being used anywhere in the program.</p>



<a name="204376049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204376049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204376049">(Jul 19 2020 at 23:48)</a>:</h4>
<p>For what it's worth, it looks to me that the MIR output isn't deleting those lines: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=release&amp;edition=2018&amp;gist=203ffc8a5e3fc4cee393bc0737ec8b42">https://play.rust-lang.org/?version=nightly&amp;mode=release&amp;edition=2018&amp;gist=203ffc8a5e3fc4cee393bc0737ec8b42</a></p>



<a name="204376050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204376050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204376050">(Jul 19 2020 at 23:48)</a>:</h4>
<p>So it looks like LLVM doing all that work of deleting "dead code"</p>



<a name="204376111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204376111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204376111">(Jul 19 2020 at 23:51)</a>:</h4>
<p>yeah its something LTO can achieve.</p>



<a name="204376114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204376114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204376114">(Jul 19 2020 at 23:51)</a>:</h4>
<p>or probably thinLTO</p>



<a name="204376222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204376222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204376222">(Jul 19 2020 at 23:54)</a>:</h4>
<p>Ohh, you mean that maybe LTO can do that proof and delete the code? Damn, LLVM is a beast</p>



<a name="204376226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/mir%20generated%20for%20declarations%20inside%20unreachable%20code%3F/near/204376226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/mir.20generated.20for.20declarations.20inside.20unreachable.20code.3F.html#204376226">(Jul 19 2020 at 23:54)</a>:</h4>
<p>Makes sense tho, LTO is where you'd try to do it</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>